import datetime
from typing import List, Type

from sqlalchemy.orm import Session

from core.entity.do.models import CmsGaodeTraceDay
from core.entity.vo.view_models import CmsGaodeTraceVO
from models import CmsOrderExecute, CmsGaodeTraceDay


class CmsGaodeTraceDayDao:
    """
    高德终端dao
    """

    @classmethod
    def get_cms_gaode_trace_day_day_by_id(cls, db: Session, id: int):
        """
        根据id查询高德终端信息
        :param db: orm对象
        :param id: 主键ID
        :return: 高德终端信息对象
        """
        cms_gaode_trace_day = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.id == id) \
            .first()

        return cms_gaode_trace_day

    @classmethod
    def get_cms_gaode_trace_day_by_trace_id(cls, db: Session, trace_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param trace_id: 终端id
        :return: 高德终端信息对象
        """
        cms_gaode_trace_day = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.trace_id == trace_id) \
            .first()

        return cms_gaode_trace_day

    @classmethod
    def get_cms_gaode_trace_day_running(cls, db: Session):
        cms_gaode_trace_day_all: List[Type[CmsGaodeTraceDay]] = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.distance is None or CmsGaodeTraceDay.distance.driver_id.strip() == "") \
            .all()

        return cms_gaode_trace_day_all

    @classmethod
    def get_cms_gaode_trace_day_by_name(cls, db: Session, trace_name: str):
        """
        获取所有的参数配置信息
        :param db: orm对象
        :param trace_name: 终端名称
        :return: 高德终端信息对象集合
        """
        cms_gaode_trace_day_list = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.trace_name == trace_name) \
            .first()

        return cms_gaode_trace_day_list

    @classmethod
    def get_cms_gaode_trace_day_like_name(cls, db: Session, trace_name: str):
        """
        获取所有的参数配置信息
        :param db: orm对象
        :param trace_name: 终端名称
        :return: 高德终端信息对象集合
        """
        cms_gaode_trace_day_list = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.trace_name.like(f'%{trace_name}%') if trace_name else True) \
            .all()

        return cms_gaode_trace_day_list

    @classmethod
    def add_cms_gaode_trace_day_dao(cls, db: Session, cms_gaode_trace_day: CmsGaodeTraceVO):
        """
        新增高德终端
        :param db: orm对象
        :param cms_gaode_trace_day: 高德终端对象
        :return:
        """
        model_dict = CmsGaodeTraceDay(**cms_gaode_trace_day.dict())
        db.add(model_dict)
        db.flush()

        return model_dict

    @classmethod
    def delete_cms_gaode_trace_day_dao(cls, db: Session, id: int):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param id: id
        :return:
        """
        db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.id == id) \
            .delete()

    @classmethod
    def get_cms_gaode_trace_day_by_execute_id(cls, db: Session, execute_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param trace_id: 终端id
        :return: 高德终端信息对象
        """
        cms_gaode_trace_day = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.execute_id == execute_id) \
            .all()

        return cms_gaode_trace_day

    @classmethod
    def get_cms_gaode_trace_day_by_execute_id_and_time(cls, db: Session, execute_id: str):
        now = datetime.datetime.now()
        split = now.replace(hour=4, minute=0, second=0, microsecond=0)
        if now > split:
            start_time = now.replace(hour=4, minute=0, second=0, microsecond=0)
            end_time = (now + datetime.timedelta(days=1)).replace(hour=4, minute=0, second=0, microsecond=0)
        else:
            start_time = (now - datetime.timedelta(days=1)).replace(hour=4, minute=0, second=0, microsecond=0)
            end_time = now.replace(hour=4, minute=0, second=0, microsecond=0)
        cms_gaode_trace_day = db.query(CmsGaodeTraceDay) \
            .filter(CmsGaodeTraceDay.execute_id == execute_id) \
            .filter(CmsGaodeTraceDay.create_time > start_time.strftime('%Y-%m-%d %H:%M:%S')) \
            .filter(CmsGaodeTraceDay.create_time < end_time.strftime('%Y-%m-%d %H:%M:%S')) \
            .all()

        return cms_gaode_trace_day


